package com.shopyourstyle.activities;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.shopyourstyle.R;

public class CharacterActivity extends Activity implements SurfaceHolder.Callback{
	private String MSG_TAG = "CharacterActivity";
	private Button nextBtn = null;
	private Button takeBtn = null;
	private Camera myCamera = null;
	private SurfaceView previewSurfaceView = null;
	private SurfaceHolder previewSurfaceHolder = null;
	private boolean previewing = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.character);
        Log.d(MSG_TAG, "OnCreate()");
        
        nextBtn = (Button)findViewById(R.id.character_next);
        nextBtn.setOnClickListener(nextBtnLs);
        takeBtn = (Button)findViewById(R.id.character_take);
        takeBtn.setOnClickListener(takeBtnLs);
        
        getWindow().setFormat(PixelFormat.UNKNOWN);     
        previewSurfaceView = (SurfaceView)findViewById(R.id.character_camera);
        previewSurfaceHolder = previewSurfaceView.getHolder();
        previewSurfaceHolder.addCallback((Callback) this);
        previewSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    
	private Button.OnClickListener nextBtnLs=new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(CharacterActivity.this,MainActivity.class);
			startActivity(intent);
		}
	};
	
	private Button.OnClickListener takeBtnLs=new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			myCamera.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);
		}
	};
	
	private ShutterCallback shutterCallback = new ShutterCallback(){
		 @Override
		 public void onShutter() {
//			 This is called when the image is actually taken
		 }};
		 
	private PictureCallback rawPictureCallback = new PictureCallback(){
		@Override
		public void onPictureTaken(byte[] arg0, Camera arg1) {
		}};
		 
	private PictureCallback jpegPictureCallback = new PictureCallback(){
		@Override
		public void onPictureTaken(byte[] arg0, Camera arg1) {
		}};
		 
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		if(previewing){
			myCamera.stopPreview();
			previewing = false;
		}
		 
		try{
			myCamera.setPreviewDisplay(arg0);
			myCamera.startPreview();
			previewing = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		 
	public void surfaceCreated(SurfaceHolder arg0) {
		myCamera = Camera.open();
		myCamera.setDisplayOrientation(90);
		try{
			Camera.Parameters cameraParameters = myCamera.getParameters();			
			cameraParameters.setRotation(90);
			myCamera.setDisplayOrientation(90);			
			myCamera.setPreviewDisplay(previewSurfaceHolder);
		}
		catch (IOException exception){
			myCamera.release();
		}
	}
		 
	public void surfaceDestroyed(SurfaceHolder arg0) {
		myCamera.stopPreview();
		myCamera.release();
		myCamera = null;
		previewing = false;
	}
}